home *** CD-ROM | disk | FTP | other *** search
- /* setjmp.c --- BIBLE pp. 93-94 */
- #include <stdio.h>
- #include <setjmp.h>
- static jmp_buf this_place;
- static void fake_error(void);
- main()
- {
- int retval;
- retval = setjmp(this_place);
- if(retval == 0)
- {
- printf("First return from \"setjmp\"\n");
- }
- else
- {
- printf("Second return from \"setjmp\" induced by call "
- "to \"longjmp\"\n");
- /* Do processing that's otherwise skipped.
- For example, error recovery. */
- printf("There may be an error handler here.\n "
- "We simply exit.\n");
- exit(retval);
- }
- /* Somewhere else, in another function call longjmp */
- printf("Everything seemed fine until suddenly...\n");
- fake_error();
- }
- /* ---------------------------------- */
- static void fake_error(void)
- {
- printf("Illegal instruction\n");
- printf("--- longjmp called ---\n");
- longjmp(this_place, 1);
- }